import torch
import torch.nn as nn
import torch.optim as optim

# 定义一个简单的神经网络
model = nn.Sequential(
    nn.Linear(10, 50),
    nn.ReLU(),
    nn.Linear(50, 1)
)

# 定义损失函数和优化器，并添加L1正则化
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, weight_decay=0.001)  # weight_decay即为L1正则化系数

# 训练循环
for epoch in range(num_epochs):
    for inputs, targets in data_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, targets) + sum(p.abs().sum() for p in model.parameters()) * 0.001  # 手动添加L1正则化项
        loss.backward()
        optimizer.step()

